/**
 * Created by duwei on 2018/12/1.
 */
var CoinShopType = {
    FangKa:1,
    Coin:2,
    Diamond:3
}
var CoinShopWindow = Window.extend({
    _ccsFileName: res.CoinShopWindow_json,
    PAY_QMDPURL:'https://pay.yayayouxi.com',
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'root.close': '_clickClose'
        };
    },
    _initNetworkListeners: function () {
        this._eventListeners[EventKey.UpdateFk] = "initUserInfo";
        cc.eventManager.addCustomListener("update3013WithCocosNtf", this.onNoticeUpdate.bind(this));
        cc.eventManager.addCustomListener("removeShopWithCocosNtf", this._clickClose.bind(this));
    },
    /**
     * 刷新数据，外部调用setData
     */
    refreshData:function (_ciType) {
        var self = this;
        network.send(2103, {cmd: 'updateLocal',  location: 3});
        self.initUserInfo();
        self.showH5Shop(_ciType);
    },
    showH5Shop: function (ciType) {//ciType   房卡是1   金币是2   钻石是3
        var self = this;
        var uid = gameData.uid;
        var area = app.area;
        var signKey = Crypto.MD5("feiyu-pay" + uid + area);
        var indexUrl =  self.PAY_QMDPURL + "/payServer/wxpay/app_pay/dist/index.html?";
        //var indexUrl =  self.PAY_QMDPURL + "/payServer-test/wxpay/app_pay/dist/index.html?";
        indexUrl+="area=" + area + "&buyerId=" + gameData.uid + "&source=5&ciType="+ciType+"&signKey=" + signKey+"&#http://";
        cc.log("indexUrl==="+indexUrl);
        var sizeX = 1280;
        var sizeY = 641;
        var webView = new ccui.WebView(indexUrl);
        webView.setContentSize(sizeX, sizeY);
        webView.setAnchorPoint(0.5, 0);
        webView.setPosition(cc.p(cc.winSize.width/2,0));
        self.addChild(webView);
    },
    initUserInfo: function () {
        var self = this;
        if (gameData.numOfCards && gameData.numOfCards[1] >= 0) {
            gameData.cardnum = gameData.numOfCards[1];
            self._nodeList['root.roomcard.txtCount'].setString(equalNum(gameData.cardnum) || "0");
        }
        if (gameData.numOfCards && gameData.numOfCards[0] >= 0) {
            gameData.coinnum = gameData.numOfCards[0];
            self._nodeList['root.roomcoin.txtCount'].setString(equalNum(gameData.coinnum) || "0");
        }
        if (gameData.numOfCards && gameData.numOfCards[2] >= 0) {
            gameData.diamondnum = gameData.numOfCards[2];
            self._nodeList['root.roomdiamond.txtCount'].setString(equalNum(gameData.diamondnum) || "0");
        }
    },
    _clickClose:function () {
        var self = this;
        self.removeFromParent();
        network.send(3013,{});
        network.send(2103, {cmd: 'updateLocal',  location: 0});
    },
    onNoticeUpdate: function (event) {
        if (!event){
            return;
        }
        var data = event.getUserData();
        if (!data){
            return;
        }
        var numOfCards = data["numof_cards"];
        if (numOfCards){
            gameData.numOfCards = numOfCards;
            this.initUserInfo();
        }
    }
});